// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ControlScript from "../PublicScript/ControlScript";
import GetSkin from "./GetSkin";
import GameData_red_blue_man, {emitType} from "./GameData_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";

const {ccclass, property} = cc._decorator;

@ccclass
export default class EasterEgg extends cc.Component {

    @property({displayName: "彩蛋对应的皮肤编号"})
    eggIndex: number = 0;
    @property({displayName: "需要观看视频的次数"})
    videoCount: number = 0;

    onLoad () {
        this.node.active = false;
        this.init();
        cc.game.on(emitType.getEggSkin, ()=>{this.node.active = false;}, this);
        cc.game.on(emitType.eggSkinPage, ()=>{this.hasPage = false;}, this);
    }

    start () {

    }

    hasPage = false;
    createPage(){
        //判断上一个界面是否还在 ，还在就不做处理
        if(this.hasPage){
            return
        }
        this.hasPage = true;
        Music.GamePlayEffect(musicName.daoju);
        this.scheduleOnce(()=>{
            ControlScript.instantiate("getSkin", (res)=>{
                res.getComponent(GetSkin).setInitData(GameData_red_blue_man.skinData[this.eggIndex].name,2, true, this.videoCount, this.eggIndex);
            })
        }, 0.2);

    }

    onBeginContact(contact, self, other) {
        if(other.tag != 0 || GameData_red_blue_man.isGuiding){
            return;
        }
        this.createPage();
    }

    init(){
        if(GameData_red_blue_man.eggCount[this.eggIndex] >= this.videoCount){
            this.node.active = false;
        }
        if(!GameData_red_blue_man.eggCount[this.eggIndex]){
            GameData_red_blue_man.eggCount[this.eggIndex] = 0;
            GameData_red_blue_man.saveEggCount();
        }
    }

    // update (dt) {}
}
